package com.dkts.oil.util.gis;

import com.uber.h3core.H3Core;
import com.uber.h3core.util.GeoCoord;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.*;


/**
 * @author lgd liguodong@itbox.cn
 * @date 2024-05-15
 * @description: 六角格工具类
 * @version: 1.0.0
 */

@Slf4j
public class HexUtil {
    public static volatile H3Core h3Core;

    public static H3Core getInstance() {
        if (h3Core == null) {
            synchronized (HexUtil.class) {
                if(h3Core == null){
                    try {
                        h3Core = H3Core.newInstance();
                    }catch (IOException e) {
                        log.warn("h3Core init error", e);
                    }
                }
            }
        }
        return h3Core;
    }

    public static void main(String[] args) {
        System.out.println(HexUtil.h3ToRing("8a2a107f9bfffff", 1));
//116.912927 40.617465
       String h3 = geoToH3Address(116.912927, 40.617465, 7);
        System.out.println(h3);
    }

     public static List<String> getChildHexList(List<String> sixthLayerHexagons){
         List<String> seventhLayerHexagons = new ArrayList<>();

         for (String sixthLayerHex : sixthLayerHexagons) {
             // 使用h3ToChildren方法获取每个第六层索引的所有第七层子索引
             List<String> childrenHexes = HexUtil.h3ToChildren(sixthLayerHex, 7);
             seventhLayerHexagons.addAll(childrenHexes);
         }
         return seventhLayerHexagons;
     }

    /**
     * 获取六边形列表
     * @param h3 六边形地址
     * @param childRes 获取子六边形数量
     * @return 六边形列表
     */
    public static List<String> h3ToRing(String h3,Integer childRes) {
        try {
            return getInstance().hexRing(h3, childRes);
        }catch (Exception e) {
            log.error("h3To error", e);
        }
        return null;
    }

    public static List<String> h3ToChildren(String h3,Integer childRes) {

        return getInstance().h3ToChildren(h3,childRes);
    }

    /**
     * 获取六边形列表
     * @param lat 纬度
     * @param lng 经度
     * @param res 获取子六边形数量
     * @return 六边形列表
     */
    public static String geoToH3Address(double lat, double lng, int res) {

        return getInstance().geoToH3Address(lat,lng,res);
    }

    public static List<GeoCoord> h3ToGeoBoundary(String h3) {

        return getInstance().h3ToGeoBoundary(h3);
    }

    /**
     * 获取六边形中心点
     * @param h3Address 六边形地址
     * @return Geo
     */
    public static GeoCoord getGeoCoord(String h3Address) {

        return getInstance().h3ToGeo(h3Address);
    }




}
